package com.project.personnel.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.nio.charset.StandardCharsets;

//重置数据库
@RestController
@RequestMapping("/reset_database")
public class ResetDatabaseController {
    @GetMapping("/reset")
    public boolean ResetDatabase() {
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec("mysql -uroot -p111111 --default-character-set=utf8 personnel");
            OutputStream outputStream = process.getOutputStream();
            FileInputStream fis = new FileInputStream("d:\\PersonnelConfig\\personnel.sql");
            InputStreamReader isr = new InputStreamReader(fis, StandardCharsets.UTF_8);
            BufferedReader br = new BufferedReader(isr);
            String str;
            StringBuilder sb = new StringBuilder();
            while ((str = br.readLine()) != null) {
                sb.append(str).append("\r\n");
            }
            str = sb.toString();
            OutputStreamWriter writer = new OutputStreamWriter(outputStream, StandardCharsets.UTF_8);
            writer.write(str);
            writer.flush();
            writer.close();
            br.close();
            isr.close();
            fis.close();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
